from typing import Any

from zkl_pyutils_registries import UNSET

from zkl_pyutils_serialization.basics import context, get_context_tag_value, get_context_tags
from zkl_pyutils_serialization.tags import ContextualRegistry, DstFormat, DstTypehint, Format, SrcFormat, SrcType, \
    SrcTypehint
from .registry import registry


def convert(
    src: Any, *,
    src_typehint: Any = UNSET,
    dst_typehint: Any = UNSET,
    src_format: type[Format] | None = None,
    dst_format: type[Format] | None = None,
) -> Any:
    context_entries = {}
    context_entries[SrcType] = type(src)
    context_entries[SrcTypehint] = src_typehint
    context_entries[DstTypehint] = dst_typehint
    if src_format is not None:
        context_entries[SrcFormat] = src_format
    if dst_format is not None:
        context_entries[DstFormat] = dst_format

    with context(context_entries):
        contextual_registry = get_context_tag_value(ContextualRegistry, registry)
        return contextual_registry.match(get_context_tags())(src)
